#pragma once

#include <AVUIRoutedEventHandlerInfo.h>

namespace AVUI {

typedef ItemStructList< TRefCountedPtr<Delegate> > EventStorage;

class EventRoute;

class EventHandlersStore : public Object
{
    friend class UIElement;
public:
#pragma region Internal Methods

    EventHandlersStore() { };
    void Add(Object* pEvent, Delegate* pHandler);
    void AddRoutedEventHandler(RoutedEvent* pRoutedEvent, Delegate* pHandler, bool handledEventsToo);
    void ClearAll(Object* pEvent);
    bool Contains(RoutedEvent* pRoutedEvent);

    TArrayPtr<RoutedEventHandlerInfo> GetRoutedEventHandlers(RoutedEvent* pRoutedEvent);

    void Remove(Object* pEvent, Delegate* pHandler);
    void RemoveRoutedEventHandler(RoutedEvent* pRoutedEvent, Delegate* pHandler);

    void Invoke(Object* pvKey, Object* pSender, Object* pArgs);
    void InvokeRoutedEventHandler(RoutedEvent* pRoutedEvent, Object* pSender, RoutedEventArgs* pArgs);

    void AddInstanceHandlersToRoute(EventRoute* pRoute);
    void AddHandlersToStorage(Object* pEvent, EventStorage* pStorage);

    bool get_IsEmpty() { return m_eventListeners.get_Count() == 0; }

#pragma endregion Internal Methods

private:

#pragma region Private Fields

    struct EventList
    {
        void* Key;
        ItemStructList<RoutedEventHandlerInfo> Listeners;
    };

    
    ItemStructList<EventList> m_eventListeners;

#pragma endregion Private Fields

};

}; // namespace AVUI
